package com.artqiyi.dollmachine.views.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.artqiyi.dollmachine.R;
import com.artqiyi.dollmachine.common.Base.BaseActivity;
import com.artqiyi.dollmachine.common.constant.UserConfigConstant;
import com.artqiyi.dollmachine.model.entity.ConfigBean;
import com.artqiyi.dollmachine.model.entity.UserInfoBean;
import com.artqiyi.dollmachine.presenter.LoginPresenter;
import com.artqiyi.dollmachine.presenter.base.PresenterCallbackCollection;
import com.artqiyi.dollmachine.utils.AppUtils;
import com.artqiyi.dollmachine.utils.manager.ConfigManager;
import com.artqiyi.dollmachine.utils.manager.UserInfoManager;
import com.artqiyi.dollmachine.views.widget.ToolBarView;
import com.artqiyi.dollmachine.views.widget.dialog.RxDialogSureCancel;
import com.artqiyi.dollmachine.wxapi.WXEntryActivity;

import butterknife.Bind;
import timber.log.Timber;

/**
 * author: PengCheng
 * time: 2017/12/13 0013
 * desc:
 */

public class LoginActivity extends BaseActivity<LoginPresenter> implements View.OnClickListener, PresenterCallbackCollection.LoginActivity{

    @Bind(R.id.btnLogin)
    TextView btnLogin;

    @Override
    public LoginPresenter getPresenter() {
        return new LoginPresenter();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void handleToolBar(ToolBarView toolBar) {
        super.handleToolBar(toolBar);
        toolBar.setVisibility(View.GONE);
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        btnLogin.setOnClickListener(this);
    }

    @Override
    public void loadData() {
        super.loadData();
        if (!TextUtils.isEmpty(UserConfigConstant.getToken())) {
            Timber.d("token:" + UserConfigConstant.getToken());
            UserInfoManager.getInstance().setToken(UserConfigConstant.getToken());
            mPresenter.getUserInfo();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnLogin:
                mPresenter.doLogin();
                break;
        }
    }

    @Override
    public void onGetUserInfoSuccess(UserInfoBean bean) {
        UserInfoManager.getInstance().setUserInfoBeanWithoutToken(bean);
        mPresenter.getConfig();
    }

    //token无效
    @Override
    public void onGetTokenInvalid() {

    }

    @Override
    public void onGetUserInfoFail(String msg) {
        showToast("获取个人信息失败");
    }

    @Override
    public void onConfigSuccess(ConfigBean bean) {
        if (null != bean) {
            ConfigManager.getInstance().setConfigBean(bean);
//            ConfigManager.getInstance().setFakePay(bean.getFake_pay());
//            ConfigManager.getInstance().setMallUrl(bean.getMall_url());
//            ConfigManager.getInstance().setSocketServerHost(bean.getSocket_server_host());
            ConfigManager.getInstance().setClosePay(TextUtils.equals(AppUtils.getVersionName(LoginActivity.this), bean.getAuditing_version()));
//            ConfigManager.getInstance().setApp_name(bean.getApp_name());
            startActivity(new Intent(LoginActivity.this, MainActivity.class));
            finish();
        }
    }

    @Override
    public void onConfigFail(String msg) {
        Toast.makeText(LoginActivity.this, msg, Toast.LENGTH_SHORT).show();
    }
}
